package com.ruoyi.web.controller.hatdevice.fence;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.device.domain.HatDeviceOrderLogs;
import com.ruoyi.device.domain.req.Instructions.SendInstructionsReq;
import com.ruoyi.device.domain.req.fence.ElectricFenceAddReq;
import com.ruoyi.device.domain.req.fence.ElectricFenceEditReq;
import com.ruoyi.device.domain.req.fence.ElectricFenceReq;
import com.ruoyi.device.domain.req.hatdevice.HatDeviceAddReq;
import com.ruoyi.device.domain.req.hatdevice.HatDeviceEditReq;
import com.ruoyi.device.domain.resp.fence.ElectricFenceResp;
import com.ruoyi.device.domain.resp.hatdevice.HatDeviceByIdResp;
import com.ruoyi.device.service.ElectricFenceService;
import com.ruoyi.device.service.HatDeviceOrderLogsService;
import com.ruoyi.system.domain.SysPost;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author wy
 * @Description 电子围栏
 * @Date 2023/12/2 11:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/device/fence")
public class ElectricFenceController extends BaseController {

    @Autowired
    private ElectricFenceService  electricFenceService;
    @Autowired
    private HatDeviceOrderLogsService hatDeviceOrderLogsService;

    /**
     * 电子围栏查询
     * @param req
     * @return
     */
    @PreAuthorize("@ss.hasPermi('device:fence:list')")
    @GetMapping("/data")
    public TableDataInfo list(ElectricFenceReq req){
        startPage();
        List<ElectricFenceResp> list = electricFenceService.search(req);
        System.out.printf("========list:"+list);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('device:fence:add')")
    @PostMapping
    public Boolean add(@Valid @RequestBody ElectricFenceAddReq req){
        Boolean result = this.electricFenceService.add(req);
        return result;
    }

    @PreAuthorize("@ss.hasPermi('device:fence:edit')")
    @PutMapping
    public Boolean edit(@Valid @RequestBody ElectricFenceEditReq req){
        return this.electricFenceService.edit(req);
    }


    @PreAuthorize("@ss.hasPermi('device:fence:query')")
    @GetMapping("/{id}")
    public ElectricFenceResp findById(@PathVariable Long id) {
        ElectricFenceResp byid = this.electricFenceService.findByid(id);
        return byid;
    }

    @PreAuthorize("@ss.hasPermi('device:fence:remove')")
    @DeleteMapping("/{id}")
    public Boolean delete (@PathVariable Long[] id){
        System.out.printf("============="+id);
        return this.electricFenceService.updateDel(id);
    }

    @PreAuthorize("@ss.hasPermi('device:fence:edit')")
    @GetMapping("/status/{id}")
    public Boolean editStatus(@PathVariable Long id){
        return this.electricFenceService.editStatus(id);
    }

    /**
     * 发送指令
     * @return
     */
    @PreAuthorize("@ss.hasPermi('device:map:list')")
    @PostMapping("/send")
    public R sendInstructions(@RequestBody SendInstructionsReq sendInstructionsReq){
        return this.hatDeviceOrderLogsService.sendInstructions(sendInstructionsReq)?R.ok():R.fail("发送失败！");
    }

}
